<template>
  <div class="app-container">
    <!-- 搜索栏 -->
    <el-card class="search-wrapper">
      <el-form :inline="true" :model="queryParams" @keyup.enter="handleQuery">
        <el-form-item label="医生姓名">
          <el-input v-model="queryParams.name" placeholder="请输入医生姓名" clearable />
        </el-form-item>
        <el-form-item label="所属科室">
          <el-select v-model="queryParams.deptId" placeholder="请选择科室" clearable>
            <el-option
              v-for="dept in deptOptions"
              :key="dept.id"
              :label="dept.name"
              :value="dept.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
          <el-button @click="resetQuery">重置</el-button>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 数据表格 -->
    <el-card class="table-wrapper">
      <el-table v-loading="loading" :data="list">
        <el-table-column label="医生姓名" prop="realName" />
        <el-table-column label="所属科室" prop="deptName" />
        <el-table-column label="职称" prop="title" />
        <el-table-column label="专长" prop="specialty" show-overflow-tooltip />
        <el-table-column label="认证状态" align="center">
          <template #default="{ row }">
            <el-tag :type="getCertificationStatus(row.certificationStatus).type">
              {{ getCertificationStatus(row.certificationStatus).label }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="250">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleUpdate(row)">编辑</el-button>
            <el-button type="success" link @click="handleCertification(row)" v-if="row.certificationStatus === 0">
              认证
            </el-button>
            <el-button type="danger" link @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <pagination
        v-if="total > 0"
        :total="total"
        v-model:page="queryParams.current"
        v-model:limit="queryParams.size"
        @pagination="getList"
      />
    </el-card>

    <!-- 添加/编辑弹窗 -->
    <el-dialog
      :title="form.id ? '编辑医生' : '新增医生'"
      v-model="dialog.visible"
      width="600px"
      append-to-body
    >
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="医生姓名" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入医生姓名" />
        </el-form-item>
        <el-form-item label="所属科室" prop="deptId">
          <el-select v-model="form.deptId" placeholder="请选择科室">
            <el-option
              v-for="dept in deptOptions"
              :key="dept.id"
              :label="dept.name"
              :value="dept.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="职称" prop="title">
          <el-input v-model="form.title" placeholder="请输入职称" />
        </el-form-item>
        <el-form-item label="专长" prop="specialty">
          <el-input v-model="form.specialty" placeholder="请输入专长" />
        </el-form-item>
        <el-form-item label="简介" prop="introduction">
          <el-input
            v-model="form.introduction"
            type="textarea"
            :rows="3"
            placeholder="请输入简介"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialog.visible = false">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getDoctorList, getDoctor, addDoctor, updateDoctor, deleteDoctor, certificationDoctor } from '@/api/system/doctor'
import { getDepartmentList } from '@/api/system/department'

const loading = ref(false)
const total = ref(0)
const list = ref([])
const deptOptions = ref([])
const formRef = ref()

const queryParams = reactive({
  current: 1,
  size: 10,
  name: '',
  deptId: undefined
})

const dialog = reactive({
  visible: false
})

const form = reactive({
  id: undefined,
  realName: '',
  deptId: undefined,
  title: '',
  specialty: '',
  introduction: ''
})

const rules = {
  realName: [{ required: true, message: '请输入医生姓名', trigger: 'blur' }],
  deptId: [{ required: true, message: '请选择所属科室', trigger: 'change' }],
  title: [{ required: true, message: '请输入职称', trigger: 'blur' }]
}

const getCertificationStatus = (status) => {
  const statusMap = {
    0: { label: '待认证', type: 'info' },
    1: { label: '已认证', type: 'success' },
    2: { label: '认证失败', type: 'danger' }
  }
  return statusMap[status] || { label: '未知', type: 'warning' }
}

// 获取科室列表
const getDeptList = async () => {
  const { data } = await getDepartmentList({ size: 100 })
  deptOptions.value = data.records
}

// 获取医生列表
const getList = async () => {
  try {
    loading.value = true
    const { data } = await getDoctorList(queryParams)
    list.value = data.records
    total.value = data.total
  } finally {
    loading.value = false
  }
}

// 查询
const handleQuery = () => {
  queryParams.current = 1
  getList()
}

// 重置
const resetQuery = () => {
  queryParams.name = ''
  queryParams.deptId = undefined
  handleQuery()
}

// 新增
const handleAdd = () => {
  reset()
  dialog.visible = true
}

// 修改
const handleUpdate = async (row) => {
  reset()
  const { data } = await getDoctor(row.id)
  Object.assign(form, data)
  dialog.visible = true
}

// 认证
const handleCertification = (row) => {
  ElMessageBox.confirm('确认要通过该医生的认证吗？', '提示', {
    type: 'warning'
  }).then(async () => {
    await certificationDoctor(row.id, 1)
    ElMessage.success('认证成功')
    getList()
  })
}

// 删除
const handleDelete = (row) => {
  ElMessageBox.confirm('确认要删除该医生吗？', '警告', {
    type: 'warning'
  }).then(async () => {
    await deleteDoctor(row.id)
    ElMessage.success('删除成功')
    getList()
  })
}

// 表单重置
const reset = () => {
  form.id = undefined
  form.realName = ''
  form.deptId = undefined
  form.title = ''
  form.specialty = ''
  form.introduction = ''
}

// 表单提交
const submitForm = async () => {
  await formRef.value.validate()
  
  if (form.id) {
    await updateDoctor(form)
    ElMessage.success('修改成功')
  } else {
    await addDoctor(form)
    ElMessage.success('新增成功')
  }
  
  dialog.visible = false
  getList()
}

onMounted(() => {
  getDeptList()
  getList()
})
</script>

<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}
</style> 